package com.wteam.dragon.model.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.model.mapper.JobDtoMapper;
import com.wteam.dragon.model.mapper.JobMapper;
import com.wteam.dragon.model.pojo.bean.Job;
import com.wteam.dragon.model.pojo.dto.JobDto;
import com.wteam.dragon.model.service.JobService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 陈晓辉
 * @Date 2020/4/2 9:38
 */
@Service
@RequiredArgsConstructor
public class JobServiceImpl implements JobService {
    private final JobMapper jobMapper;
    private final JobDtoMapper jobDtoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addJob(Job job) {
        jobMapper.insert(job);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delJob(Long id) {
        ValidationUtil.isNull("id", id);
        jobMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editJob(Job job) {
        jobMapper.updateById(job);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更新成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageJob(Page<JobDto> page, Long deptId, String jobName) {
        QueryWrapper<JobDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(jobName != null, JobDto::getName, jobName)
                .eq(JobDto::getId, deptId)
                .orderByAsc(JobDto::getSort);
        Page<JobDto> jobPage = jobDtoMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("jobList", jobPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage listJobName() {
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        List<Job> jobList = jobMapper.selectList(queryWrapper);
        List<String> jobNameList = jobList.stream().map(Job::getName).collect(Collectors.toList());
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("jobNameList", jobNameList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

}
